一度に複数の Rive を読み込む場合はキャッシュしておけば UI ハングしない
よく探せばちゃんと公式に書いてあった; Caching a Rive File - RiveI
Under most circumstances a
.rivfile should load quickly and managing theRiveFileyourself is not necessary. But if you intend to use the same.rivfile in multiple parts of your application, or even on the same screen, it might be advantageous to load the file once and keep it in memory.
公式も、複数を同時に使う場合は有効と書いてある。
自分の場合は、同じ Rive をボタンに仕込んで、そのボタンが 1 つの画面に 8 つ登場していた。
その画面に遷移しようとすると 0.5–1.0 sec くらい hang が起きていた。
それを、以下のような実装に変更しただけで解決した。ファイルを読み込むのに時間がかかるんだね。
let sharedFile = try! RiveFile(
name: "name_of_riv",
in: .module // マルチモジュールなので
)
class SomeView: UIView {
private let viewModel = RiveViewModel(
RiveModel(riveFile: sharedFile),
stateMachineName: "name_of_state_machine",
autoPlay: false,
artboardName: "name_of_artboard"
)
}
追記
↑ の方法だと、グローバル変数は遅延初期化されるので起動直後の読み込みなどでカクつくことがあった。
Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties.